/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgUtil.pkgFrameworkXML.pkgSupport;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import net.sf.cglib.core.Constants;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderJDOMFactory;
import org.jdom2.input.sax.XMLReaderXSDFactory;
import pkgUtil.pkgFrameworkXML.pkgEntity.RXmlObject;

/**
 *
 * @author Administrador
 */
public class HelperReadXml {

    private RXmlObject xmlObject;
    private SAXBuilder saxBuilder;
    private XMLReaderJDOMFactory rFactory;
    private Document xmlDocument;

    public HelperReadXml(RXmlObject xmlObject) throws JDOMException, FileNotFoundException, IOException, XmlReaderException, URISyntaxException, Exception {
        validateXmlObject(xmlObject);
        this.xmlObject = xmlObject;
        this.saxBuilder = new SAXBuilder();
        this.buildDocument();
    }

    private void validateXmlObject(RXmlObject xmlObject) throws XmlReaderException {
        if (xmlObject == null) {
            throw new XmlReaderException("XmlObject is null");
        }
        if (xmlObject.getFileXml() == null) {
            throw new XmlReaderException("Path file Xml is null");
        }

        if (xmlObject.getFileXml().trim().length() == 0) {
            throw new XmlReaderException("Path file Xml is not valid");
        }
    }

    private void buildDocument() throws JDOMException, FileNotFoundException, IOException, URISyntaxException, Exception {
        if (xmlObject.isValidate()) {
            InputStream inStFileXSD;
            javax.xml.transform.Source srcXSD = null;
            try {
                inStFileXSD = getClass().getResourceAsStream(xmlObject.getFileXsd());
                srcXSD = new StreamSource(inStFileXSD);
            } catch (Exception ex) {
                throw new Exception("Error al intentar cargar validador XSD para configuración regional. (" + ex.getMessage() + ")");
            }
            rFactory = new XMLReaderXSDFactory(srcXSD);
            saxBuilder.setXMLReaderFactory(rFactory);
        }
        xmlDocument = saxBuilder.build(new FileInputStream(xmlObject.getFileXml()));
    }

    /**
     * @return the xmlObject
     */
    public RXmlObject getXmlObject() {
        return xmlObject;
    }

    public Element getRootElement() {
        return xmlDocument.getRootElement();
    }

    public Element getElementFromRoot(String child) {
        return getRootElement().getChild(child);
    }

    public Element getChild(Element parent, String child) {
        return parent.getChild(child);
    }

    public String getChildTextFromRoot(String child) {
        return getRootElement().getChildText(child);
    }

    public String getChildText(Element parent, String child) {
        return parent.getChildText(child);
    }

    public String getElementValue(Element element) {
        return element.getValue();
    }
}
